var categoryData = [
    {
        "categoryId": 1,
        "categoryName": "Java",
        "parentId": 0
    },
    {
        "categoryId": 8,
        "categoryName": "Java基础",
        "parentId": 1
    },
    {
        "categoryId": 13,
        "categoryName": "JavaOOP",
        "parentId": 8
    },
    {
        "categoryId": 19,
        "categoryName": "继承",
        "parentId": 13
    },
    {
        "categoryId": 20,
        "categoryName": "封装",
        "parentId": 13
    },
    {
        "categoryId": 21,
        "categoryName": "多态",
        "parentId": 13
    },


    {
        "categoryId": 14,
        "categoryName": "Java反射",
        "parentId": 8
    },

    {
        "categoryId": 2,
        "categoryName": "Python",
        "parentId": 0
    },
    {
        "categoryId": 9,
        "categoryName": "Python基础",
        "parentId": 2
    },
    {
        "categoryId": 15,
        "categoryName": "Python字典",
        "parentId": 9
    },
    {
        "categoryId": 16,
        "categoryName": "爬虫",
        "parentId": 9
    },

    {
        "categoryId": 3,
        "categoryName": "UI",
        "parentId": 0
    },
    {
        "categoryId": 10,
        "categoryName": "UI设计基础",
        "parentId": 3
    },
    {
        "categoryId": 17,
        "categoryName": "PS",
        "parentId": 10
    },
    {
        "categoryId": 18,
        "categoryName": "AE",
        "parentId": 10
    },


    {
        "categoryId": 4,
        "categoryName": "JavaScript",
        "parentId": 0
    },
    {
        "categoryId": 11,
        "categoryName": "ES6规范",
        "parentId": 4
    },
    {
        "categoryId": 22,
        "categoryName": "DOM",
        "parentId": 11
    },

    {
        "categoryId": 5,
        "categoryName": "MySQL",
        "parentId": 0
    },
    {
        "categoryId": 12,
        "categoryName": "MySQL基础",
        "parentId": 5
    },

    {
        "categoryId": 6,
        "categoryName": "Network",
        "parentId": 0
    },
    {
        "categoryId": 7,
        "categoryName": "QA",
        "parentId": 0
    }
];
var teacherData = [
    {"tid": 1, "tname": "艾老师"},
    {"tid": 2, "tname": "白老师"},
    {"tid": 3, "tname": "陈老师"},
    {"tid": 4, "tname": "邓老师"},
    {"tid": 5, "tname": "额老师"},
    {"tid": 6, "tname": "房老师"},
    {"tid": 7, "tname": "锅老师"},
    {"tid": 8, "tname": "黄老师"},
    {"tid": 9, "tname": "i老师"},
    {"tid": 10, "tname": "金老师"},
];
var cpIndex = 0;
var coursewareNum = 0;

class Chapter {
    constructor(orderNum, testId, cpname) {
        this.orderNum = orderNum;
        this.testId = testId;
        this.cpname = cpname;
        this.newCwIds = [];
    }
}

class Courseware {
    constructor(orderNum, ncid, ncname, cpname, ncformat, duration) {
        this.orderNum = orderNum;
        this.ncid = ncid;
        this.ncname = ncname;
        this.cpname = cpname;
        this.ncformat = ncformat;
        this.duration = duration;
    }
}

var addCourse = new Vue({
    el: "#addCourse",
    data: {
        h: headers,
        stepOne: "block",
        stepTwo: "none",
        stepThree: "none",
        cname: "",
        description: "",
        cover: "",
        categories: [],
        coursewares: [],
        testes: [],
        //category
        selected: 0,
        //img data
        image: "",
        tagName: "",
        tagList: [],
        tagIndex: 0,
        isSeries: 1,
        teachers: [],
        selectedT: 0,
        selectExam: 0,
        testId: 0,
        currentCourseware: {},
        newCoursewares: [],
        newTestes: [],
        currentChapter: {},
        selectChapterIndex: 0,
        chapterIndex: 0,
        //章节名称
        chapterName: "",
        chapters: [],
        cstatus: "",
        chooseType: "",
        // openComment: "",
        // openLikes: "",
        // openNotes: "",
        // openPush: "",
        // noHangUp: "",
        checkedStatus: "checkedStatus",
        mult_check: "mult_check",
        openCommentFlag: false,
        openLikesFlag: false,
        openNotesFlag: false,
        openPushFlag: false,
        noHangUpFlag: false,
        examId: 0,
        coursewareNum: 0,
        course: {}
    },
    mounted() {
        console.info("headers:")
        console.info(this.h)
        axios({
            method: "get",
            url: "/course/courseCategory/all",
            headers: this.h
        }).then(res => {
            console.info("category-res:")
            console.info(res)
            if (res.data.code === 200) {
                this.categories = res.data.data;
            }
        }).catch(err => {
            console.info(err)
        });
        axios({
            method: "get",
            url: "/course/allTeachers",
            headers: this.h
        }).then(res => {
            console.info("teacher-res:")
            console.info(res)
            this.teachers = res.data.data;
        }).catch(err => {
            console.info(err)
        });
        axios({
            method: "get",
            url: "/course/courseware/all",
            headers: this.h
        }).then(res => {
            console.info("coursewares-res:")
            console.info(res)
            if (res.data.code === 200) {
                this.coursewares = res.data.data;
            }
        }).catch(err => {
            console.info(err)
        });
        axios({
            method: "get",
            url: "/exam/paper/all",
            headers: this.h
        }).then(res => {
            console.info("testes-res:")
            console.info(res)
            this.testes = res.data.data;
        }).catch(err => {
            console.info(err)
        });
    },
    methods: {
        postTagList:function(cid){
            console.info("标签cid："+cid)
            let tagList = this.tagList;
        },
        postChapters:function(cid){
            console.info("有没有cid ； "+cid);
            let chapters = this.chapters;
            for (let i = 0; i < chapters.length; i++) {
                this.chapters[i].cid = cid;
            }
            return axios({
                method:"post",
                url:"/course/addChapters",
                data: this.chapters
            })
        },
        putInfoIntoChapter: function () {
            let chapters = this.chapters;
            let newCoursewares = this.newCoursewares;
            for (let i = 0; i < chapters.length; i++) {
                console.info("试卷id：" + this.newTestes[i].id)
                this.chapters[i].testId = this.newTestes[i].id;
                for (let j = 0; j < newCoursewares.length; j++) {
                    if (chapters[i].orderNum === newCoursewares[j].orderNum) {
                        console.info("新课件的id ： " + this.newCoursewares[j].ncid)
                        this.chapters[i].newCwIds.push(this.newCoursewares[j].ncid)
                    }
                }
            }
            console.info("chapters: =========================")
            console.info(this.chapters)
        },
        putCourse: function (n) {
            this.cstatus = n;//发布状态
            this.putInfoIntoChapter();
            this.encapsulation();
            console.info("标签列表：");
            console.info(this.tagList);
            if (this.course === {} || null || undefined) {
                alert("数据为空，请认真填写")
                return;
            }

            axios.post("/course/newCourse",this.course,{
                headers: {
                    'Content-Type': 'application/json;charset=UTF-8',
                    "token":token,
                    "refreshToken":refreshToken
                }
            }).then(res => {
                console.info(res)
                alert(res.data.message);
                if (res.data.code === 200) {
                    this.course = {};
                    window.location.href = "/cs/newCourse.html";
                }
            }).catch(err => {
                console.info(err)
            });
        },
        openHangup: function () {
            this.noHangUpFlag = !this.noHangUpFlag;
        },
        openPush: function () {
            this.openPushFlag = !this.openPushFlag;
        },
        openNote: function () {
            this.openNotesFlag = !this.openNotesFlag;
        },
        openLike: function () {
            this.openLikesFlag = !this.openLikesFlag;
        },
        openC: function () {
            this.openCommentFlag = !this.openCommentFlag;
        },
        chooseT: function (n) {
            this.chooseType = '' + n;
        },
        delTest: function (n) {
            this.newTestes.splice(n, 1);//splice(index,num to delete,item... to add)
            console.info(this.newTestes)
        },
        addNewExam: function () {
            let divElement = document.createElement("div");
            divElement.className = "tag";
            let test = this.testes[this.selectExam];
            this.newTestes.push(test);
            console.info(this.newTestes)
        },
        delCourseware: function (n) {
            this.newCoursewares.splice(n, 1);//splice(index,num to delete,item... to add)
            console.info(this.newCoursewares)
        },
        addNewCourseware: function (n) {
            let cw = this.coursewares[n];
            let chapter = this.chapters[this.selectChapterIndex];
            let cwid = cw.cwid;
            let cwname = cw.cwname;
            let format = cw.format;
            let duration = cw.duration;
            let cpname = chapter.cpname;
            let orderNum = chapter.orderNum;
            console.info("=================新添加课件的信息================")
            console.info("selectChapterIndex : " + this.selectChapterIndex)
            console.info("cwid : " + cwid)
            console.info("cwname : " + cwname)
            console.info("format : " + format)
            console.info("cpname : " + cpname)
            console.info("orderNum : " + orderNum)
            console.info("duration : " + duration)
            let courseware = new Courseware(orderNum, cwid, cwname, cpname, format, duration);
            this.newCoursewares.push(courseware);
            console.info(this.newCoursewares)
        },
        delChapter: function (n) {
            this.chapters.splice(n, 1);//splice(index,num to delete,item... to add)
            //不重新排序，新增的章节编号就乱了重复了
            for (let i = 0; i < this.chapters.length; i++) {
                this.chapters[i].orderNum = i;
            }
            console.info(this.chapters)
        },
        encapsulateChapter: function () {
            let chapter = new Chapter(this.chapters.length, 0, this.chapterName);
            console.info(chapter);
            this.chapters.push(chapter);
            console.info(this.chapters)
            this.chapterName = "";
        },
        encapsulation: function () {
            this.course = {
                "cname": this.cname,
                "description": this.description,
                "category": this.selected,
                "tid": this.selectedT,
                "cstatus": this.cstatus,
                "cover": this.cover,
                "isSeries": this.isSeries,
                "chooseType": this.chooseType,
                "openComment": this.openCommentFlag ? '1' : '0',
                "openLikes": this.openLikesFlag ? '1' : '0',
                "openNotes": this.openNotesFlag ? '1' : '0',
                "openPush": this.openPushFlag ? '1' : '0',
                "noHangUp": this.noHangUpFlag ? '1' : '0',
                "examId": this.examId,
                "coursewareNum": this.newCoursewares.length,
                "courseTags": this.tagList,
                "courseChapters": this.chapters
            };
            console.info("查看课程数据封装：");
            console.info(this.course);
            console.info("查看json字符串：")
            console.info(JSON.stringify(this.course))
        },
        whichTeacher: function () {
            console.info("第几个老师： " + this.selectedT);//tid
        },
        check: function (n) {
            console.info(n === 1 ? "系列课程" : "单课程");
            this.isSeries = n;
        },
        finishAddTag: function () {
            let divElement = document.createElement("div");
            divElement.className = "tag";
            let tagName = this.tagName;
            this.tagList[this.tagIndex++] = tagName;
            divElement.innerText = tagName;
            $("#addTag").before(divElement);
            this.tagName = "";
            console.info(this.tagList)
        },
        changeImg: function (e) {
            this.image = e.target.files[0];//文件数据绑定
            let formData1 = new FormData();
            formData1.append('upload',this.image);
            axios({
                method: 'post',
                url: "http://117.51.156.132:8080/upload/uploadImage",
                data: formData1,
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            }).then(res =>{
                console.info("返回文件名：")
                console.info(res)
                console.info(res.data.name)
                this.cover = res.data.name;
            }).catch(err =>{
                console.info(err)
            });
            this.cover = this.image.name;
        },
        selectWho: function () {
            console.info(this.selected)//categoryId
        },
        gotoStepOne: function () {
            this.stepOne = "block";
            this.stepTwo = "none";
            this.stepThree = "none";
            this.encapsulation();
        },
        gotoStepTwo: function () {
            this.stepOne = "none";
            this.stepTwo = "block";
            this.stepThree = "none";
            this.encapsulation();
        },
        gotoStepThree: function () {
            this.stepOne = "none";
            this.stepTwo = "none";
            this.stepThree = "block";
            this.encapsulation();
        }
    }
});
``